const koa = require("koa")

const app = new koa()

const session = require("koa-session")

const redisStore = require("koa-redis")

const redis = require("redis")

const redisClient = redis.createClient(6379, "localhost")

const wrapper = require("co-redis")

const client = wrapper(redisClient)

app.keys = ["some secret"]

app.use(
  session(
    {
      key: "zxc:sess",
      store: redisStore({
        client
      })
    },
    app
  )
)

//配置项

// const SESS_CONFIG = {
//   key: "test:sess",
//   maxAge: 86400000,
//   httpOnly: true,
//   signed: true
// }

// //注册
// app.use(session(SESS_CONFIG, app))

app.use(async (ctx, next) => {
  const keys = await client.keys("*")
  keys.forEach(async key => {
    console.log(await client.get(key))
  })
  await next()
})

app
  .use(ctx => {
    if (ctx.path === "/favicon") return
    //获取
    let n = ctx.session.count || 0
    //设置
    ctx.session.count = ++n

    ctx.body = `第${n}次访问`
  })
  .listen(3000)
